﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GIMS.BLL;
using KPIS.GERP.GIMS.IBLL;
using KPIS.GERP.GIMS.MODEL;
using System.Data;
using System.IO;

namespace KPIS.GERP.GIMS.APP.APP_UI.Add_Form
{
    public partial class ICM_Delivery_Note : System.Web.UI.Page
    {
        Common.Main cn = new Common.Main();
        private bool isItemInEditMode = false;
        CommandControl cmdFac = new CommandControl();
        IBLLPersonal bllPersonal = new PersonalBLL();
        PurchaseOrderInstallmentBLL bllPoInstall = new PurchaseOrderInstallmentBLL();
        IBLLMasterWarehouse bllWh = new MasterWarehouseBLL();
        DeliveryNoteItemBLL bllDnItem = new DeliveryNoteItemBLL();
        DeliveryNoteCommitteeBLL bllDnCommittee = new DeliveryNoteCommitteeBLL();
        IBLLResourceMasterItem bllResItem = new ResourceMasterItemBLL();
        IBLLMasterCommitteeFunction bllComFunction = new MasterCommitteeFunctionBLL();
        IBLLMasterCommitteePosition bllComPosition = new MasterCommitteePositionBLL();
        IBLLHrmEmployee bllEmployee = new HrmEmployeeBLL();
        PurchaseOrderPenaltyBLL bllPoPenalty = new PurchaseOrderPenaltyBLL();
        DeliveryNoteBLL bllDn = new DeliveryNoteBLL();
        double totalAmount = 0;
        double totalPoAmount = 0;
        int ItemsPerRequest = 10;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadCommitteeFunction();
                LoadCommitteePosition();
                GetPurchaseOrderDetail();
                LoadWarehouse();
                LoadGridItem();
                //LoadGridExtra();
                LoadGridCommittee();
                LoadGridPenalty();
                LoadGridFile();
                txtDate.Text = String.Format("{0:dd/MM/yyyy}", DateTime.Today);
                CalendarDate.FocusedDate = DateTime.Today;
                CalendarDate.SelectedDate = DateTime.Today;
            }
            rblExpenses.Items[0].Attributes.Add("onclick", "return WareHouse('" + cbWarehouse.ClientID + "')");
            rblExpenses.Items[1].Attributes.Add("onclick", "return Expenses('" + cbWarehouse.ClientID + "')"); 
        }

        protected int GetInstallSeq()
        {
            int seq = Convert.ToInt32(Request.QueryString["install_seq"]);
            return seq;
        }

        protected int GetPoSeq()
        {
            int seq = Convert.ToInt32(Request.QueryString["po_seq"]);
            return seq;
        }

        protected void RadManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            if (e.Argument == "Penalty")
            {
                LoadPenaltyConclusion();
            }
        }

        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }

        #region Tab ข้อมูลหลัก

        protected void GetPurchaseOrderDetail()
        {
            PurchaseOrderInstallmentInfo info = bllPoInstall.GetPurchaseOrderInstallmentFromDeliveryNote(GetInstallSeq());

            lblPoType.Text = info.InfoPurchaseOrder.InfoPoType.Name;
            lblPoRefNo.Text = info.InfoPurchaseOrder.Ref_No;
            lblPoName.Text = info.InfoPurchaseOrder.Name;
            panelInstallment.Visible = (info.InstallNumber != null);
            lblInstallment.Text = info.InstallNumber.ToString(); ;
            lblSeller.Text = info.InfoPurchaseOrder.InfoBidder.InfoMasContact.Name_th;
            lblRedemptionAmount.Text = (info.RedemptionAmount != null ? Convert.ToDouble(info.RedemptionAmount).ToString("###,###,##0.00") : "0");
            lblDeductionAmount.Text = (info.DeductionAmount != null ? Convert.ToDouble(info.DeductionAmount).ToString("###,###,##0.00") : "0");

            txtDateDelivery.Text = (info.DeliveryDate != null ? String.Format("{0:dd/MM/yyyy}", info.DeliveryDate) : "");
            lblDeliveryDate.Text = (info.DeliveryDate != null ? String.Format("{0:dd/MM/yyyy}", info.DeliveryDate) : "");
            hdYearType.Value = info.InfoPurchaseOrder.InfoYearType.Seq.ToString();
            hdBgmYear.Value = info.InfoPurchaseOrder.BgmYear.ToString();
        }

        protected void LoadWarehouse()
        { 
            IList<MasterWarehouseInfo> infoList = bllWh.GetMasterWarehouse();
            cbWarehouse.DataSource = infoList;
            cbWarehouse.DataBind();
        }

        protected void nextButton_Detail_Click(object sender, EventArgs e)
        {
            RadManager1.ResponseScripts.Add(@"ChangeTab('Item',1);");
        }

        #endregion

        #region tab รายการพัสดุตรวจรับ

        protected void LoadGridItem()
        {
            IList<DeliveryNoteItemInfo> infoList = new List<DeliveryNoteItemInfo>();

            if (ViewState["Item"] != null)
            {
                infoList = (List<DeliveryNoteItemInfo>)ViewState["Item"];
            }
            else
            {
                infoList = bllDnItem.GetPurchaseOrderInstallmentItemForDeliveryNote(GetInstallSeq());
                ViewState["Item"] = infoList;
            }

            GridGroupByExpression ge = new GridGroupByExpression(Grid_Item.Columns[0]);
            Grid_Item.MasterTableView.GroupByExpressions.Add(ge);
            Grid_Item.DataSource = infoList;
            Grid_Item.DataBind();
            Edit_Item.Visible = true;
            EditSave_Item.Visible = false;
            Cancel_Item.Visible = false;
        }

        protected void Grid_Item_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadGridItem();
        }

        protected void Grid_Item_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                double price = 0;
                double poPrice = 0;
                GridDataItem dataBoundItem = e.Item as GridDataItem;

                double pricePerQty = Convert.ToDouble(dataBoundItem["PriceInVat"].Text);
                RadNumericTextBox txtQty = ((RadNumericTextBox)(dataBoundItem["Qty"].FindControl("txtQty")));
                RadNumericTextBox txtNetAmount = ((RadNumericTextBox)(dataBoundItem["NetAmount"].FindControl("txtNetAmount")));

                txtQty.Attributes.Add("onFocus", "return FocusQty('" + txtQty.ClientID + "','" + txtNetAmount.ClientID + "','" + pricePerQty + "')");
                txtQty.Attributes.Add("onBlur", "return BlurQty('" + txtQty.ClientID + "','" + txtNetAmount.ClientID + "','" + pricePerQty + "')");
                txtNetAmount.Attributes.Add("onFocus", "return FocusPrice('" + txtNetAmount.ClientID + "')");
                txtNetAmount.Attributes.Add("onBlur", "return BlurPrice('" + txtNetAmount.ClientID + "')");

                if (((RadNumericTextBox)(dataBoundItem["NetAmount"].FindControl("txtNetAmount"))).Value.ToString().Length > 0)
                {
                    price = double.Parse(((RadNumericTextBox)(dataBoundItem["NetAmount"].FindControl("txtNetAmount"))).Value.ToString());
                }

                if (((Label)(dataBoundItem["InfoPoInstallItem.NetAmount"].FindControl("lblPoNetAmount"))).Text.ToString().Length > 0)
                {
                    poPrice = double.Parse(((Label)(dataBoundItem["InfoPoInstallItem.NetAmount"].FindControl("lblPoNetAmount"))).Text.ToString());
                }

                totalAmount += price;
                totalPoAmount += poPrice;
            }
            else if (e.Item is GridFooterItem)
            {
                GridFooterItem footer = (GridFooterItem)e.Item;
                ((Label)footer["InfoPoInstallItem.NetAmount"].FindControl("lblTotalPoNetAmount")).Text = String.Format("{0:N3}", totalPoAmount);
                ((Label)footer["NetAmount"].FindControl("lblTotalNetAmount")).Text = String.Format("{0:N3}", totalAmount);
                ((RadNumericTextBox)footer["NetAmount"].FindControl("txtTotalNetAmount")).Value = Double.Parse(totalAmount.ToString());
            }
        }

        protected void Edit_Item_Click(object sender, EventArgs e)
        {
            this.IsItemInEditMode = true;
            LoadGridItem();
            Edit_Item.Visible = false;
            EditSave_Item.Visible = true;
            Cancel_Item.Visible = true;
        }

        protected void EditSave_Item_Click(object sender, EventArgs e)
        {
            IList<DeliveryNoteItemInfo> infoList = new List<DeliveryNoteItemInfo>();
            foreach (GridDataItem data in Grid_Item.MasterTableView.Items)
            {
                DeliveryNoteItemInfo info = new DeliveryNoteItemInfo();
                PurchaseOrderInstallmentItemInfo infoInstallItem = new PurchaseOrderInstallmentItemInfo();
                BidderQuotationInfo infoBidderQuotation = new BidderQuotationInfo();
                SysVatInfo infoVat = new SysVatInfo();
                ResourceMasterItemInfo infoItem = new ResourceMasterItemInfo();
                PurchaseRequestItemInfo infoPrItem = new PurchaseRequestItemInfo();
                BidderInfo infoBidder = new BidderInfo();
                MasterUnitInfo infoUnit = new MasterUnitInfo();

                string vat_percent = data["InfoPoInstallItem.InfoBidderQuotation.InfoBidder.VatPercent"].Text;

                infoVat.Seq = Convert.ToInt32(data["InfoPoInstallItem.InfoBidderQuotation.InfoBidder.InfoVat.Seq"].Text);
                infoVat.Name = data["InfoPoInstallItem.InfoBidderQuotation.InfoBidder.InfoVat.Name"].Text;
                infoItem.Name = ((Label)data["InfoPoInstallItem.InfoBidderQuotation.InfoPrItem.InfoItem.Name"].FindControl("lblItem")).Text;
                infoUnit.Name = data["InfoPoInstallItem.InfoBidderQuotation.InfoPrItem.InfoItem.InfoUnit.Name"].Text;
                infoInstallItem.Seq = GetInstallSeq();
                info.Descr = ((TextBox)data["Descr"].FindControl("txtDescr")).Text;
                info.Qty = (((RadNumericTextBox)data["Qty"].FindControl("txtQty")).Text.Length > 0
                    ? Convert.ToInt32(((RadNumericTextBox)data["Qty"].FindControl("txtQty")).Text)
                    : 0);
                info.PriceExVat = Convert.ToDouble(data["PriceExVat"].Text);
                info.PriceInVat = Convert.ToDouble(data["PriceInVat"].Text);
                info.ExVatAmount = (Convert.ToDouble(info.Qty) * info.PriceExVat);
                info.NetAmount = (((RadNumericTextBox)data["NetAmount"].FindControl("txtNetAmount")).Text.Length > 0
                    ? Convert.ToInt32(((RadNumericTextBox)data["NetAmount"].FindControl("txtNetAmount")).Text)
                    : 0);
                info.VatAmount = Math.Round(Convert.ToDouble(info.NetAmount * (Convert.ToDouble(vat_percent) / (Convert.ToDouble(vat_percent) + 100))), 3);
                infoBidder.VatPercent = Convert.ToDouble(vat_percent);
                infoInstallItem.Qty = (data["InfoPoInstallItem.Qty"].Text.Length > 0
                    ? Convert.ToInt32(data["InfoPoInstallItem.Qty"].Text)
                    : 0);
                infoInstallItem.NetAmount = (((Label)data["InfoPoInstallItem.NetAmount"].FindControl("lblPoNetAmount")).Text.Length > 0
                    ? Convert.ToDouble(((Label)data["InfoPoInstallItem.NetAmount"].FindControl("lblPoNetAmount")).Text)
                    : 0);

                infoItem.InfoUnit = infoUnit;
                infoPrItem.InfoItem = infoItem;
                infoBidder.InfoVat = infoVat;
                infoBidderQuotation.InfoPrItem = infoPrItem;
                infoBidderQuotation.InfoBidder = infoBidder;
                infoInstallItem.InfoBidderQuotation = infoBidderQuotation;
                info.InfoPoInstallItem = infoInstallItem;
                infoList.Add(info);
            }
            ViewState["Item"] = infoList;
            LoadGridItem();
        }

        protected void Cancel_Item_Click(object sender, EventArgs e)
        {
            this.IsItemInEditMode = false;
            LoadGridItem();
            Edit_Item.Visible = true;
            EditSave_Item.Visible = false;
            Cancel_Item.Visible = false;
        }

        protected bool IsItemInEditMode
        {
            get { return this.isItemInEditMode; }
            set { this.isItemInEditMode = value; }
        }

        protected void backButton_Item_Click(object sender, EventArgs e)
        {
            RadManager1.ResponseScripts.Add(@"ChangeTab('Detail',0);");
        }

        protected void nextButton_Item_Click(object sender, EventArgs e)
        {
            RadManager1.ResponseScripts.Add(@"ChangeTab('Committee',2);");
        }

        #endregion

        #region tab คณะกรรมการ

        protected void LoadGridCommittee()
        {
            IList<DeliveryNoteCommitteeInfo> infoList = new List<DeliveryNoteCommitteeInfo>();
            if (ViewState["Committee"] != null)
            {
                infoList = (List<DeliveryNoteCommitteeInfo>)ViewState["Committee"];
            }
            else
            {
                infoList = bllDnCommittee.GetDeliveryNoteCommitteeFromPurchaseOrder(GetPoSeq());
                ViewState["Committee"] = infoList;
            }
            Grid_Committee.DataSource = infoList;
            GridGroupByExpression geFunction = new GridGroupByExpression(Grid_Committee.Columns[2]);
            GridGroupByExpression geRefNo = new GridGroupByExpression(Grid_Committee.Columns[3]);
            Grid_Committee.MasterTableView.GroupByExpressions.Add(geFunction);
            Grid_Committee.MasterTableView.GroupByExpressions.Add(geRefNo);
            Grid_Committee.DataBind();

            cmdFac.GridCommand(panelList_Committee, panelGrid_Committee, panelCommand_Committee);
        }

        protected void LoadCommitteeFunction()
        {
            IList<MasterCommitteeFunctionInfo> infoList = bllComFunction.GetMasterCommitteeFunction();
            cbCommitteeFunction.DataSource = infoList;
            cbCommitteeFunction.DataBind();
        }

        protected void LoadCommitteePosition()
        {
            IList<MasterCommitteePositionInfo> infoList = bllComPosition.GetMasterCommitteePosition();
            cbCommitteePosition.DataSource = infoList;
            cbCommitteePosition.DataBind();
        }

        protected void Grid_Committee_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadGridCommittee();
        }

        protected void Grid_Committee_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                GridDataItem dataBoundItem = e.Item as GridDataItem;
                Image image = (Image)dataBoundItem["Image"].FindControl("Image1");
                if (dataBoundItem["InfoRecord.InfoStatus.Status"].Text == "W")
                {
                    image.ImageUrl = "../Image/Grid/Update.gif";
                }
                else
                {
                    image.ImageUrl = "../Image/Grid/Cancel.gif";
                }
            }
        }

        protected void rblCommitteeType_SelectedIndexChanged(object sender, EventArgs e)
        {
            panelInternal.Visible = (rblCommitteeType.SelectedValue == "1");
            panelExternal.Visible = (rblCommitteeType.SelectedValue == "2");
            LoadValidationCommittee();
        }

        protected void cbCommitteeInternalName_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            IList<HrmEmployeeInfo> infoList = bllEmployee.GetHrmEmployee(e.Text);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, infoList.Count);
            e.EndOfItems = (endOffset == infoList.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cbCommitteeInternalName.Items.Add(new RadComboBoxItem(infoList[i].Name, infoList[i].Seq.ToString()));
                TableRow tr = new TableRow();
                TableCell tdJobSite = new TableCell();
                Label lblJobSite = new Label();
                lblJobSite.Text = infoList[i].InfoJobSite.Name;
                tdJobSite.Width = Unit.Pixel(200);
                tdJobSite.Controls.Add(lblJobSite);
                tr.Cells.Add(tdJobSite);
                TableCell tdName = new TableCell();
                Label lblName = new Label();
                lblName.Text = infoList[i].Name;
                tdName.Width = Unit.Pixel(150);
                tdName.Controls.Add(lblName);
                tr.Cells.Add(tdName);
                TableCell tdPosition = new TableCell();
                Label lblPosition = new Label();
                lblPosition.Text = infoList[i].InfoPosition.Name;
                tdPosition.Width = Unit.Pixel(150);
                tdPosition.Controls.Add(lblPosition);
                tr.Cells.Add(tdPosition);
                TableCell tdClass = new TableCell();
                Label lblClass = new Label();
                lblClass.Text = infoList[i].InfoClass.Name;
                tdClass.Width = Unit.Pixel(190);
                tdClass.Controls.Add(lblClass);
                tr.Cells.Add(tdClass);
                ((Table)cbCommitteeInternalName.Items[j].FindControl("tableCommmitteeInternalName")).Controls.Add(tr);
                j++;
            }

            e.Message = GetStatusMessage(endOffset, infoList.Count);
        }

        protected void AddNew_Committee_Click(object sender, EventArgs e)
        {
            cmdFac.AddNewCommand(panelList_Committee, panelGrid_Committee, panelCommand_Committee);
            LoadValidationCommittee();
        }

        protected void btnDelete_Committee_Click(object sender, EventArgs e)
        {
            IList<DeliveryNoteCommitteeInfo> infoListCommittee = new List<DeliveryNoteCommitteeInfo>();
            if (ViewState["Committee"] != null)
            {
                infoListCommittee = (List<DeliveryNoteCommitteeInfo>)ViewState["Committee"];
            }
            int[] data_value = new int[Grid_Committee.MasterTableView.GetSelectedItems().Length];
            int item = Grid_Committee.MasterTableView.GetSelectedItems().Length;
            foreach (GridDataItem data in Grid_Committee.MasterTableView.GetSelectedItems())
            {
                item = item - 1;
                data_value[item] = data.ItemIndex;
            }

            for (int i = 0; i < data_value.Length; i++)
            {
                infoListCommittee[data_value[i]].DeleteReason = txtDeleteReason.Text;
                infoListCommittee[data_value[i]].InfoRecord.InfoStatus.Status = SystemInfo._DeleteRecordStatus;
                infoListCommittee[data_value[i]].InfoRecord.Updated_by = Int32.Parse(Session["EmpSeq"].ToString());
                infoListCommittee[data_value[i]].InfoRecord.Updated_when = DateTime.Now;
            }

            ViewState["Committee"] = infoListCommittee;
            LoadGridCommittee();
        }

        protected void Delete_Committee_Click(object sender, EventArgs e)
        {
            int[] data_value = new int[Grid_Committee.MasterTableView.GetSelectedItems().Length];
            int item = Grid_Committee.MasterTableView.GetSelectedItems().Length;
            foreach (GridDataItem data in Grid_Committee.MasterTableView.GetSelectedItems())
            {
                item = item - 1;
                data_value[item] = data.ItemIndex;
            }

            for (int i = 0; i < data_value.Length; i++)
            {
                ((List<DeliveryNoteCommitteeInfo>)ViewState["Committee"]).RemoveAt(data_value[i]);
            }

            LoadGridCommittee();
        }

        protected void AddNewSave_Committee_Click(object sender, EventArgs e)
        {
            IList<DeliveryNoteCommitteeInfo> infoListCommittee = new List<DeliveryNoteCommitteeInfo>();
            if (ViewState["Committee"] != null)
            {
                infoListCommittee = (List<DeliveryNoteCommitteeInfo>)ViewState["Committee"];
            }

            DeliveryNoteCommitteeInfo info = new DeliveryNoteCommitteeInfo();
            HrmEmployeeInfo infoEmployee = new HrmEmployeeInfo();
            MasterCommitteePositionInfo infoPosition = new MasterCommitteePositionInfo();
            MasterCommitteeFunctionInfo infoFunction = new MasterCommitteeFunctionInfo();
            HrmEmployeeInfo infoEmployeeDetail = new HrmEmployeeInfo();
            SysCommitteeTypeInfo infoType = new SysCommitteeTypeInfo();
            RecordInfo infoRecord = new RecordInfo();
            SysRecordStatusInfo infoStatus = new SysRecordStatusInfo();

            info.Ref_no = Convert.ToInt32(txtCommitteeRefno.Value);
            infoType.Seq = Convert.ToInt32(rblCommitteeType.SelectedValue);
            infoType.Name = (rblCommitteeType.SelectedItem.Text);
            infoPosition.Seq = Convert.ToInt32(cbCommitteePosition.SelectedValue);
            infoPosition.Name = cbCommitteePosition.SelectedItem.Text;
            infoFunction.Seq = Convert.ToInt32(cbCommitteeFunction.SelectedValue);
            infoFunction.Name = cbCommitteeFunction.SelectedItem.Text;
            if (infoType.Seq == 1) //เป็นบุคลากรภายใน
            {
                infoEmployee.Seq = Convert.ToInt32(cbCommitteeInternalName.SelectedValue);
                infoEmployee.Name = cbCommitteeInternalName.Text;
                infoEmployeeDetail = bllEmployee.GetHrmEmployee(Convert.ToInt32(infoEmployee.Seq));
                info.Name = infoEmployee.Name;
                info.Descr = (infoEmployeeDetail.InfoJobSite.Name.Length > 0 ? " <b>หน่วยงาน : </b>" + infoEmployeeDetail.InfoJobSite.Name : "")
                    + (infoEmployeeDetail.InfoPosition.Name.Length > 0 ? " <b>ตำแหน่ง :</b>" + infoEmployeeDetail.InfoPosition.Name : "")
                    + (infoEmployeeDetail.InfoClass.Name.Length > 0 ? " <b>ระดับ :</b>" + infoEmployeeDetail.InfoClass.Name : "");
            }
            else
            {
                info.Ex_name = txtCommitteeExternalName.Text;
                info.Ex_descr = txtCommitteeExternalDescr.Text;
                info.Name = info.Ex_name;
                info.Descr = info.Ex_descr;
            }
            infoStatus.Status = SystemInfo._WorkRecordStatus;
            infoRecord.Created_by = cn.UserID;
            infoRecord.Created_when = DateTime.Now;
            info.InfoType = infoType;
            info.InfoExployee = infoEmployee;
            info.InfoPosition = infoPosition;
            info.InfoFunction = infoFunction;
            infoRecord.InfoStatus = infoStatus;
            info.InfoRecord = infoRecord;
            infoListCommittee.Add(info);
            ResetCommittee();
            ViewState["Committee"] = infoListCommittee;
            LoadGridCommittee();
        }

        protected void ResetCommittee()
        {
            txtCommitteeRefno.Text = "";
            cbCommitteeInternalName.ClearSelection();
            cbCommitteeInternalName.Text = "";
            txtCommitteeExternalName.Text = "";
            txtCommitteeExternalDescr.Text = "";
            cbCommitteePosition.ClearSelection();
            cbCommitteePosition.Text = "";
            cbCommitteeFunction.ClearSelection();
            cbCommitteeFunction.Text = "";
        }

        protected void ShowGrid_Committee_Click(object sender, EventArgs e)
        {
            cmdFac.GridCommand(panelList_Committee, panelGrid_Committee, panelCommand_Committee);
        }

        protected void LoadValidationCommittee()
        {
            RequiredFieldValidator valCommitteeRefno = new RequiredFieldValidator();
            valCommitteeRefno.ID = "ValtxtCommitteeRefno";
            valCommitteeRefno.ControlToValidate = "txtCommitteeRefno";
            valCommitteeRefno.SetFocusOnError = true;
            valCommitteeRefno.Display = ValidatorDisplay.Dynamic;
            valCommitteeRefno.ErrorMessage = "* กรุณาระบุชุดที่ *";
            panelValtxtCommitteeRefno.Controls.Add(valCommitteeRefno);
            RequiredFieldValidator valCommitteeInternalName = new RequiredFieldValidator();
            valCommitteeInternalName.ID = "ValcbCommitteeInternalName";
            valCommitteeInternalName.ControlToValidate = "cbCommitteeInternalName";
            valCommitteeInternalName.SetFocusOnError = true;
            valCommitteeInternalName.Display = ValidatorDisplay.Dynamic;
            valCommitteeInternalName.ErrorMessage = "* กรุณาระบุชื่อกรรมการ *";
            panelValcbCommitteeInternalName.Controls.Add(valCommitteeInternalName);
            RequiredFieldValidator valCommitteeExternalName = new RequiredFieldValidator();
            valCommitteeExternalName.ID = "ValtxtCommitteeExternalName";
            valCommitteeExternalName.ControlToValidate = "txtCommitteeExternalName";
            valCommitteeExternalName.SetFocusOnError = true;
            valCommitteeExternalName.Display = ValidatorDisplay.Dynamic;
            valCommitteeExternalName.ErrorMessage = "* กรุณาระบุชื่อกรรมการ *";
            panelValtxtCommitteeExternalName.Controls.Add(valCommitteeExternalName);
            RequiredFieldValidator valCommitteePosition = new RequiredFieldValidator();
            valCommitteePosition.ID = "ValcbCommitteePosition";
            valCommitteePosition.ControlToValidate = "cbCommitteePosition";
            valCommitteePosition.SetFocusOnError = true;
            valCommitteePosition.Display = ValidatorDisplay.Dynamic;
            valCommitteePosition.ErrorMessage = "* กรุณาระบุตำแหน่งกรรมการ *";
            panelValcbCommitteePosition.Controls.Add(valCommitteePosition);
            RequiredFieldValidator valCommitteeFunction = new RequiredFieldValidator();
            valCommitteeFunction.ID = "ValcbCommitteeFunction";
            valCommitteeFunction.ControlToValidate = "cbCommitteeFunction";
            valCommitteeFunction.SetFocusOnError = true;
            valCommitteeFunction.Display = ValidatorDisplay.Dynamic;
            valCommitteeFunction.ErrorMessage = "* กรุณาระบุหน้าที่กรรมการ *";
            panelValcbCommitteeFunction.Controls.Add(valCommitteeFunction);
        }

        protected void backButton_Committee_Click(object sender, EventArgs e)
        {
            RadManager1.ResponseScripts.Add(@"ChangeTab('Item',1);");
        }

        protected void nextButton_Committee_Click(object sender, EventArgs e)
        {
            RadManager1.ResponseScripts.Add(@"ChangeTab('Penalty',3);");
        }

        protected void OnAjaxUpdateCommittee(object sender, ToolTipUpdateEventArgs args)
        {
            this.UpdateToolTipCommittee(args.Value, args.UpdatePanel);
        }

        private void UpdateToolTipCommittee(string elementID, UpdatePanel panel)
        {
            Control ctrl = Page.LoadControl("../UserControl/TIP_Delivery_Note_Committee_History.ascx");
            //((KPIS.GERP.GIMS.APP.APP_UI.UserControl.TIP_Purchase_Order_Committee_History)ctrl).GetBidderSeq = new CallBackHandleReturnInt(GetBidderSeq);
            panel.ContentTemplateContainer.Controls.Add(ctrl);
        }

        #endregion

        #region Tab ข้อมูลบันทึกสั่งปรับ

        protected void LoadPenaltyConclusion()
        {
            lblRealDeliveryDate.Text = txtDateDelivery.TextWithLiterals;

            if (string.IsNullOrEmpty(lblDeliveryDate.Text) || string.IsNullOrEmpty(txtDateDelivery.Text))
                return;

            DateTime startDate = Convert.ToDateTime(lblDeliveryDate.Text);
            DateTime endDate = Convert.ToDateTime(lblRealDeliveryDate.Text);

            if (Convert.ToDateTime(lblRealDeliveryDate.Text).CompareTo(Convert.ToDateTime(lblDeliveryDate.Text)) == 0)
            {
                lblResultDate.Text = "ส่งมอบตรงตามกำหนดเวลา";
            }
            else if (Convert.ToDateTime(lblRealDeliveryDate.Text).CompareTo(Convert.ToDateTime(lblDeliveryDate.Text)) < 0)
            {
                lblResultDate.Text = "ส่งมอบก่อนกำหนดเป็นเวลา " + (startDate - endDate).Days.ToString() + " วัน";
            }
            else if (Convert.ToDateTime(lblRealDeliveryDate.Text).CompareTo(Convert.ToDateTime(lblDeliveryDate.Text)) > 0)
            {
                lblResultDate.Text = "ส่งมอบช้ากว่ากำหนดเป็นเวลา " + (endDate - startDate).Days.ToString() + " วัน";
            }

            Nullable<double> sumAmount = 0;
            IList<DeliveryNoteItemInfo> infoList = new List<DeliveryNoteItemInfo>();

            if (ViewState["Item"] != null)
            { 
                infoList = (List<DeliveryNoteItemInfo>)ViewState["Item"];
                foreach (DeliveryNoteItemInfo info in infoList)
                {
                    sumAmount += (info.NetAmount != null ? info.NetAmount : 0);
                }
            }

            lblItemSumAmount.Text = Convert.ToDouble(sumAmount).ToString("###,###,##0.00");
            hdSumAmount.Value = Convert.ToDouble(sumAmount).ToString("###,###,##0.00");
        }

        protected void LoadGridPenalty()
        {
            IList<DeliveryNotePenaltyInfo> infoList = new List<DeliveryNotePenaltyInfo>();
            if (ViewState["Penalty"] != null)
            {
                infoList = (List<DeliveryNotePenaltyInfo>)ViewState["Penalty"];
            }
            Grid_Penalty.DataSource = infoList;
            Grid_Penalty.DataBind();

            cmdFac.GridCommand(panelList_Penalty, panelGrid_Penalty, panelCommand_Penalty);
        }

        protected void ShowGrid_Penalty_Click(object sender, EventArgs e)
        {
            LoadGridPenalty();
        }

        protected void AddNew_Penalty_Click(object sender, EventArgs e)
        {
            cmdFac.AddNewCommand(panelList_Penalty, panelGrid_Penalty, panelCommand_Penalty);
            LoadValidatePenalty();
            lblUnitCount.Visible = true;
            lblPenaltyDescr.Visible = true;
        }

        protected void Delete_Penalty_Click(object sender, EventArgs e)
        {
            int[] data_value = new int[Grid_Penalty.MasterTableView.GetSelectedItems().Length];
            int item = Grid_Penalty.MasterTableView.GetSelectedItems().Length;
            foreach (GridDataItem data in Grid_Penalty.MasterTableView.GetSelectedItems())
            {
                item = item - 1;
                data_value[item] = data.ItemIndex;
            }

            for (int i = 0; i < data_value.Length; i++)
            {
                ((List<DeliveryNotePenaltyInfo>)ViewState["Penalty"]).RemoveAt(data_value[i]);
            }

            LoadGridPenalty();
        }

        protected void AddNewSave_Penalty_Click(object sender, EventArgs e)
        {
            IList<DeliveryNotePenaltyInfo> infoList = new List<DeliveryNotePenaltyInfo>();
            DeliveryNotePenaltyInfo info = new DeliveryNotePenaltyInfo();
            PurchaseOrderPenaltyInfo infoPoPenalty = new PurchaseOrderPenaltyInfo();
            RecordInfo infoRecord = new RecordInfo();
            if (ViewState["Penalty"] != null)
            {
                infoList = (List<DeliveryNotePenaltyInfo>)ViewState["Penalty"];
            }
            infoRecord.Created_by = cn.UserID;
            infoRecord.Created_when = DateTime.Now;

            string[] all_value = cbPenalty.SelectedValue.Split('|');
            infoPoPenalty.Seq = Convert.ToInt32(all_value[0]);
            infoPoPenalty.Name = all_value[1];
            infoPoPenalty.PenaltyDetail = all_value[2];
            info.Calculate = Convert.ToInt32(txtCalculate.Text);
            info.Amount = Convert.ToDouble(txtAmount.Text);
            info.FinalAmount = Convert.ToDouble(txtFinalAmount.Text);
            info.Remark = txtPenaltyRemark.Text;

            info.InfoRecord = info.InfoRecord;
            info.InfoPoPenalty = infoPoPenalty;
            infoList.Add(info);
            ViewState["Penalty"] = infoList;
            LoadGridPenalty();
        }

        protected void LoadValidatePenalty()
        {
            RequiredFieldValidator valPenalty = new RequiredFieldValidator();
            valPenalty.ID = "ValcbPenalty";
            valPenalty.ControlToValidate = "cbPenalty";
            valPenalty.SetFocusOnError = true;
            valPenalty.Display = ValidatorDisplay.Dynamic;
            valPenalty.ErrorMessage = "* กรุณาระบุกำหนดการปรับ *";
            panelValcbPenalty.Controls.Add(valPenalty);
            RequiredFieldValidator valAmount = new RequiredFieldValidator();
            valAmount.ID = "ValtxtFinalAmount";
            valAmount.ControlToValidate = "txtFinalAmount";
            valAmount.SetFocusOnError = true;
            valAmount.Display = ValidatorDisplay.Dynamic;
            valAmount.ErrorMessage = "* กรุณาระบุค่าปรับจริง *";
            panelValtxtFinalAmount.Controls.Add(valAmount);
            RequiredFieldValidator valCalculate = new RequiredFieldValidator();
            valCalculate.ID = "ValtxtCalculate";
            valCalculate.ControlToValidate = "txtCalculate";
            valCalculate.SetFocusOnError = true;
            valCalculate.Display = ValidatorDisplay.Dynamic;
            valCalculate.ErrorMessage = "* กรุณาระบุจำนวนที่ไม่ครบถ้วน *";
            panelValtxtCalculate.Controls.Add(valCalculate);
        }

        protected void cbPenalty_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            IList<PurchaseOrderPenaltyInfo> infoList = bllPoPenalty.GetPurchaseOrderPenaltyWithSearch(GetPoSeq(), e.Text);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, infoList.Count);
            e.EndOfItems = (endOffset == infoList.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cbPenalty.Items.Add(new RadComboBoxItem(infoList[i].Name, infoList[i].Seq.ToString()
                        + "|" + infoList[i].Name
                        + "|" + infoList[i].PenaltyDetail
                        + "|" + infoList[i].Rate.ToString()
                        + "|" + infoList[i].InfoPenaltyRateType.Name.ToString()
                        + "|" + infoList[i].InfoPenaltyCount.Name.ToString()
                        + "|" + infoList[i].Least.ToString()));
                TableRow tr = new TableRow();

                TableCell tdName = new TableCell();
                Label lblName = new Label();
                lblName.Text = infoList[i].Name;
                tdName.Width = Unit.Pixel(100);
                tdName.Controls.Add(lblName);
                tr.Cells.Add(tdName);

                TableCell tdDescr = new TableCell();
                Label lblDescr = new Label();
                lblDescr.Text = infoList[i].PenaltyDetail;
                tdDescr.Width = Unit.Pixel(250);
                tdDescr.Controls.Add(lblDescr);
                tr.Cells.Add(tdDescr);

                ((Table)cbPenalty.Items[j].FindControl("tablePenalty")).Controls.Add(tr);
                j++;
            }

            e.Message = GetStatusMessage(endOffset, infoList.Count);
        }

        protected void Grid_Penalty_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadGridPenalty();
        }

        protected void backButton_Penalty_Click(object sender, EventArgs e)
        {
            RadManager1.ResponseScripts.Add(@"ChangeTab('Committee',2);");
        }

        protected void nextButton_Penalty_Click(object sender, EventArgs e)
        {
            RadManager1.ResponseScripts.Add(@"ChangeTab('File',4);");
        }

        #endregion

        #region tab เอกสารแนบ

        protected void LoadGridFile()
        {
            IList<DocumentInfo> infoListFile = new List<DocumentInfo>();
            if (ViewState["File"] != null)
            {
                infoListFile = (List<DocumentInfo>)ViewState["File"];
            }
            Grid_File.DataSource = infoListFile;
            Grid_File.DataBind();

            cmdFac.GridCommand(panelList_File, panelGrid_File, panelCommand_File);
        }

        protected void ShowGrid_File_Click(object sender, EventArgs e)
        {
            LoadGridFile();
        }

        protected void AddNew_File_Click(object sender, EventArgs e)
        {
            cmdFac.AddNewCommand(panelList_File, panelGrid_File, panelCommand_File);
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "onclick", "VisibleWareHouse('" + rblExpenses.SelectedValue + "', '" + cbWarehouse.ClientID + "')", true);

            LoadValidateFile();
        }

        private void LoadValidateFile()
        {
            RequiredFieldValidator valFileName = new RequiredFieldValidator();
            valFileName.ID = "ValtxtFileName";
            valFileName.ControlToValidate = "txtFileName";
            valFileName.SetFocusOnError = true;
            valFileName.Display = ValidatorDisplay.Dynamic;
            valFileName.ErrorMessage = "* กรุณาระบุชื่อไฟล์ *";
            panelValtxtFileName.Controls.Add(valFileName);
            RequiredFieldValidator valAttachment = new RequiredFieldValidator();
            valAttachment.ID = "Valattachment";
            valAttachment.ControlToValidate = "attachment";
            valAttachment.SetFocusOnError = true;
            valAttachment.Display = ValidatorDisplay.Dynamic;
            valAttachment.ErrorMessage = "* กรุณาระบุที่อยู่ไฟล์ *";
            panelValattachment.Controls.Add(valAttachment);
        }

        protected void AddNewSave_File_Click(object sender, EventArgs e)
        {
            IList<DocumentInfo> infoListFile = new List<DocumentInfo>();
            FileInfo infoFile = new FileInfo(hdSourceFile.Value);
            DocumentInfo info = new DocumentInfo();
            RecordInfo infoRecord = new RecordInfo();
            if (ViewState["File"] != null)
            {
                infoListFile = (List<DocumentInfo>)ViewState["File"];
            }
            infoRecord.Created_by = Int32.Parse(Session["EmpSeq"].ToString());
            infoRecord.Created_when = DateTime.Now;
            info.File_info = infoFile;
            info.Name = txtFileName.Text;
            info.Dest_path = hdSourceFile.Value;
            info.InfoRecord = infoRecord;
            infoListFile.Add(info);
            ViewState["File"] = infoListFile;
            LoadGridFile();
        }

        protected void Delete_File_Click(object sender, EventArgs e)
        {
            int[] data_value = new int[Grid_File.MasterTableView.GetSelectedItems().Length];
            int item = Grid_File.MasterTableView.GetSelectedItems().Length;
            foreach (GridDataItem data in Grid_File.MasterTableView.GetSelectedItems())
            {
                item = item - 1;
                data_value[item] = data.ItemIndex;
            }

            for (int i = 0; i < data_value.Length; i++)
            {
                ((List<DocumentInfo>)ViewState["File"]).RemoveAt(data_value[i]);
            }

            LoadGridFile();
        }

        protected void Grid_File_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadGridFile();
        }

        protected void backButton_File_Click(object sender, EventArgs e)
        {
            RadManager1.ResponseScripts.Add(@"ChangeTab('Penalty',3);");
        }

        protected void finishButton_File_Click(object sender, EventArgs e)
        {
            RecordInfo infoRecord = new RecordInfo();
            DeliveryNoteInfo info = new DeliveryNoteInfo();
            PurchaseOrderInfo infoPo = new PurchaseOrderInfo();
            PurchaseOrderInstallmentInfo infoPoInstallment = new PurchaseOrderInstallmentInfo();
            MasterWarehouseInfo infoWarehouse = new MasterWarehouseInfo();
            SysYearTypeInfo infoYearType = new SysYearTypeInfo();
            SysRecordStatusInfo infoRecordStatus = new SysRecordStatusInfo();
            HrmEmployeeInfo infoEmployee = new HrmEmployeeInfo();
            HrmJobSiteInfo infoJobSite = new HrmJobSiteInfo();
            IList<DeliveryNoteItemInfo> infoListItem = new List<DeliveryNoteItemInfo>();
            IList<DeliveryNoteItemExtraInfo> infoListExtra = new List<DeliveryNoteItemExtraInfo>();
            IList<DeliveryNoteCommitteeInfo> infoListCommittee = new List<DeliveryNoteCommitteeInfo>();
            IList<DeliveryNotePenaltyInfo> infoListPenalty = new List<DeliveryNotePenaltyInfo>();
            IList<DocumentInfo> infoListFile = new List<DocumentInfo>();
            string lastNumber;

            if (ViewState["Item"] != null)
            {
                infoListItem = (List<DeliveryNoteItemInfo>)ViewState["Item"];
            }
            if (ViewState["Extra"] != null)
            {
                infoListExtra = (List<DeliveryNoteItemExtraInfo>)ViewState["Extra"];
            }
            if (ViewState["Committee"] != null)
            {
                infoListCommittee = (List<DeliveryNoteCommitteeInfo>)ViewState["Committee"];
            }
            if (ViewState["Penalty"] != null)
            {
                infoListPenalty = (List<DeliveryNotePenaltyInfo>)ViewState["Penalty"];
            }
            if (ViewState["File"] != null)
            {
                infoListFile = (List<DocumentInfo>)ViewState["File"];
            }

            infoRecord.Created_by = Int32.Parse(Session["EmpSeq"].ToString());
            infoRecord.Created_when = DateTime.Now;

            info.DeliveryDate = Convert.ToDateTime(txtDate.TextWithLiterals);
            info.Name = txtName.Text;
            info.ReceiveDate = Convert.ToDateTime(txtDateDelivery.TextWithLiterals);
            //infoWarehouse.Seq = Convert.ToInt32(cbWarehouse.SelectedValue);
            info.InvoiceDate = (txtDateInvoice.Text.Length > 0 ? (Nullable<DateTime>)Convert.ToDateTime(txtDateInvoice.TextWithLiterals) : null);
            info.InvoiceNumber = txtInvoiceNo.Text;
            info.DocumentNumber = txtDocumentNumber.Text;
            info.Descr = txtDescr.Text;
            info.DeliveryPlace = txtDeliveryPlace.Text;
            info.DeductionAmount = Convert.ToDouble(lblDeductionAmount.Text);
            info.RedemptionAmount = Convert.ToDouble(lblRedemptionAmount.Text);

            info.BeExpenses = Convert.ToInt32(rblExpenses.SelectedValue);
            if (rblExpenses.SelectedValue == "0")
                infoWarehouse.Seq = string.IsNullOrEmpty(cbWarehouse.SelectedValue) ? (int?)null : Convert.ToInt32(cbWarehouse.SelectedValue);
            else
                infoWarehouse.Seq = null;

            infoPo.Seq = GetPoSeq();
            infoPoInstallment.Seq = GetInstallSeq();
            infoYearType.Seq = Convert.ToInt32(hdYearType.Value);
            info.BgmYear = Convert.ToInt32(hdBgmYear.Value);
            infoEmployee.Seq = Int32.Parse(Session["EmpSeq"].ToString());
            string[] strJobSite = GetUserJobSite(Convert.ToInt32(infoEmployee.Seq));
            infoJobSite.Seq = Convert.ToInt32(strJobSite[0]);
            infoRecordStatus.Status = SystemInfo._WorkRecordStatus;

            infoRecord.InfoStatus = infoRecordStatus;
            info.InfoYearType = infoYearType;
            info.InfoRecord = infoRecord;
            info.InfoPo = infoPo;
            info.InfoPoInstallment = infoPoInstallment;
            info.InfoWarehouse = infoWarehouse;
            info.InfoDeliveryEmp = infoEmployee;
            info.InfoDeliveryJobSite = infoJobSite;

            if (rblExpenses.SelectedValue == "0")
            {
                RequiredFieldValidator valWarehouse = new RequiredFieldValidator();
                valWarehouse.ID = "ValcbWarehouse";
                valWarehouse.ControlToValidate = "cbWarehouse";
                valWarehouse.SetFocusOnError = true;
                valWarehouse.Display = ValidatorDisplay.Dynamic;
                valWarehouse.ErrorMessage = "* กรุณาระบุคลังพัสดุรับเข้า *";
                //panelValcbWarehouse.Controls.Add(valWarehouse);
            }

            try
            {
                lastNumber = bllDn.Insert(info, infoListItem, infoListExtra, infoListCommittee, infoListPenalty, infoListFile);
            }
            catch
            {
                RadManager1.Alert(SystemInfo._InsertError);
                return;
            }
            RadManager1.Alert("เลขที่รายการของท่านคือ : '" + lastNumber + "'");
            RadManager1.ResponseScripts.Add(@"CloseWindow();");
        }

        private string[] GetUserJobSite(int emp_seq)
        {
            return bllPersonal.GetUserJobSite(emp_seq);
        }

        protected void OnAjaxUpdateFile(object sender, ToolTipUpdateEventArgs args)
        {
            this.UpdateToolTipFile(args.Value, args.UpdatePanel);
        }

        private void UpdateToolTipFile(string elementID, UpdatePanel panel)
        {
            Control ctrl = Page.LoadControl("../UserControl/TIP_Delivery_Note_File_History.ascx");
            //((KPIS.GERP.GIMS.APP.APP_UI.UserControl.TIP_Purchase_Order_Committee_History)ctrl).GetBidderSeq = new CallBackHandleReturnInt(GetBidderSeq);
            panel.ContentTemplateContainer.Controls.Add(ctrl);
        }

        #endregion

        //#region tab รายการพัสดุเพิ่มเติม

        //protected void LoadGridExtra()
        //{
        //    IList<DeliveryNoteItemExtraInfo> infoList = new List<DeliveryNoteItemExtraInfo>();
        //    if (ViewState["Extra"] != null)
        //    {
        //        infoList = (List<DeliveryNoteItemExtraInfo>)ViewState["Extra"];
        //    }
        //    Grid_Extra.DataSource = infoList;
        //    Grid_Extra.DataBind();

        //    cmdFac.GridCommand(panelList_Extra, panelGrid_Extra, panelCommand_Extra);
        //}

        //protected void ShowGrid_Extra_Click(object sender, EventArgs e)
        //{
        //    LoadGridExtra();
        //}

        //protected void AddNew_Extra_Click(object sender, EventArgs e)
        //{
        //    cmdFac.AddNewCommand(panelList_Extra, panelGrid_Extra, panelCommand_Extra);
        //    LoadValidateExtra();
        //    lblItemUnit.Visible = true;
        //}

        //protected void Delete_Extra_Click(object sender, EventArgs e)
        //{
        //    int[] data_value = new int[Grid_Extra.MasterTableView.GetSelectedItems().Length];
        //    int item = Grid_Extra.MasterTableView.GetSelectedItems().Length;
        //    foreach (GridDataItem data in Grid_Extra.MasterTableView.GetSelectedItems())
        //    {
        //        item = item - 1;
        //        data_value[item] = data.ItemIndex;
        //    }

        //    for (int i = 0; i < data_value.Length; i++)
        //    {
        //        ((List<DeliveryNoteItemExtraInfo>)ViewState["Extra"]).RemoveAt(data_value[i]);
        //    }

        //    LoadGridExtra();
        //}

        //protected void AddNewSave_Extra_Click(object sender, EventArgs e)
        //{
        //    IList<DeliveryNoteItemExtraInfo> infoList = new List<DeliveryNoteItemExtraInfo>();
        //    DeliveryNoteItemExtraInfo info = new DeliveryNoteItemExtraInfo();
        //    ResourceMasterItemInfo infoResItem = new ResourceMasterItemInfo();
        //    MasterUnitInfo infoUnit = new MasterUnitInfo();
        //    RecordInfo infoRecord = new RecordInfo();
        //    if (ViewState["Extra"] != null)
        //    {
        //        infoList = (List<DeliveryNoteItemExtraInfo>)ViewState["Extra"];
        //    }
        //    infoRecord.Created_by = Int32.Parse(Session["EmpSeq"].ToString());
        //    infoRecord.Created_when = DateTime.Now;

        //    string[] all_value = cbItem.SelectedValue.Split('|');
        //    infoResItem.Seq = Convert.ToInt32(all_value[0]);
        //    infoResItem.Name = all_value[3];
        //    infoUnit.Name = all_value[2];

        //    info.Descr = txtItemDescr.Text;
        //    info.Qty = Convert.ToInt32(txtItemQty.Text);
        //    info.Price = (txtItemPrice.Text.Length > 0 ? Convert.ToDouble(txtItemPrice.Text) : 0);
        //    info.Remark = txtItemDescr.Text;

        //    infoResItem.InfoUnit = infoUnit;
        //    info.InfoRecord = info.InfoRecord;
        //    info.InfoResItem = infoResItem;
        //    infoList.Add(info);
        //    ViewState["Extra"] = infoList;
        //    LoadGridExtra();
        //}

        //protected void LoadValidateExtra()
        //{ 
        //    RequiredFieldValidator valItem = new RequiredFieldValidator();
        //    valItem.ID = "ValcbItem";
        //    valItem.ControlToValidate = "cbItem";
        //    valItem.SetFocusOnError = true;
        //    valItem.Display = ValidatorDisplay.Dynamic;
        //    valItem.ErrorMessage = "* กรุณาระบุรายการพัสดุ *";
        //    panelValcbItem.Controls.Add(valItem);
        //    RequiredFieldValidator valQty = new RequiredFieldValidator();
        //    valQty.ID = "ValtxtItemQty";
        //    valQty.ControlToValidate = "txtItemQty";
        //    valQty.SetFocusOnError = true;
        //    valQty.Display = ValidatorDisplay.Dynamic;
        //    valQty.ErrorMessage = "* กรุณาระบุจำนวน *";
        //    panelValItemQty.Controls.Add(valQty);
        //}

        //protected void cbItem_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        //{
        //    IList<ResourceMasterItemInfo> infoList = bllResItem.GetResourceMasterItemComboBoxWithSearch(e.Text);
        //    int itemOffset = e.NumberOfItems;
        //    int endOffset = Math.Min(itemOffset + ItemsPerRequest, infoList.Count);
        //    e.EndOfItems = (endOffset == infoList.Count);
        //    int j = 0;
        //    for (int i = itemOffset; i < endOffset; i++)
        //    {
        //        cbItem.Items.Add(new RadComboBoxItem(infoList[i].Name, infoList[i].Seq.ToString() + "|" + infoList[i].Descr + "|" + infoList[i].InfoUnit.Name + "|" + infoList[i].Name));
        //        TableRow tr = new TableRow();

        //        TableCell tdGroup = new TableCell();
        //        Label lblGroup = new Label();
        //        lblGroup.Text = infoList[i].InfoSubGroup.InfoGroup.Name;
        //        tdGroup.Width = Unit.Pixel(120);
        //        tdGroup.Controls.Add(lblGroup);
        //        tr.Cells.Add(tdGroup);

        //        TableCell tdSubGroup = new TableCell();
        //        Label lblSubGroup = new Label();
        //        lblSubGroup.Text = infoList[i].InfoSubGroup.Name;
        //        tdSubGroup.Width = Unit.Pixel(120);
        //        tdSubGroup.Controls.Add(lblSubGroup);
        //        tr.Cells.Add(tdSubGroup);

        //        TableCell tdName = new TableCell();
        //        Label lblName = new Label();
        //        lblName.Text = infoList[i].Name;
        //        tdName.Width = Unit.Pixel(120);
        //        tdName.Controls.Add(lblName);
        //        tr.Cells.Add(tdName);

        //        TableCell tdDescr = new TableCell();
        //        Label lblDescr = new Label();
        //        lblDescr.Text = infoList[i].Descr;
        //        tdDescr.Width = Unit.Pixel(200);
        //        tdDescr.Controls.Add(lblDescr);
        //        tr.Cells.Add(tdDescr);

        //        TableCell tdUnit = new TableCell();
        //        Label lblUnit = new Label();
        //        lblUnit.Text = infoList[i].InfoUnit.Name;
        //        tdUnit.Width = Unit.Pixel(90);
        //        tdUnit.Controls.Add(lblUnit);
        //        tr.Cells.Add(tdUnit);
        //        ((Table)cbItem.Items[j].FindControl("tableItem")).Controls.Add(tr);
        //        j++;
        //    }

        //    e.Message = GetStatusMessage(endOffset, infoList.Count);
        //}

        //private static string GetStatusMessage(int offset, int total)
        //{
        //    if (total <= 0)
        //        return "ไม่มีข้อมูล";

        //    return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        //}

        //protected void Grid_Extra_ItemCommand(object source, GridCommandEventArgs e)
        //{
        //    LoadGridExtra();
        //}

        //protected void backButton_Extra_Click(object sender, EventArgs e)
        //{
        //    RadManager1.ResponseScripts.Add(@"ChangeTab('Item',1);");
        //}

        //protected void nextButton_Extra_Click(object sender, EventArgs e)
        //{
        //    RadManager1.ResponseScripts.Add(@"ChangeTab('Committee',3);");
        //}

        //#endregion
    }
}
